package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderForm;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemList;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItem;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Author: Phani Raja Kumar
 * Date  : Feb 20, 2005
 * Time  : 3:35:37 PM
 */
public class PurchaseOrderLineItemLotDataEntryCloseCommand extends ChipTrackExtendedAction
{

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 		= "Search";
        HttpSession mSession 		= request.getSession();
        PurchaseOrderForm purchaseOrderForm = (PurchaseOrderForm) form;
        PurchaseOrderLineItemList poLineItemList = purchaseOrderForm.getPurchaseOrderLineItemList();

		try
        {
            if( poLineItemList != null && poLineItemList.size() != 0)
            {
                Iterator lineItemIter = poLineItemList.iterator();
                while( lineItemIter.hasNext() )
                {
                    PurchaseOrderLineItem poLineItem = (PurchaseOrderLineItem) lineItemIter.next();
                    PurchaseOrderLineItemLotDataEntryList poLineItemLotDataEntryList = poLineItem.getPurchaseOrderLineItemLotDataEntryList();
                    if( poLineItemLotDataEntryList != null && poLineItemLotDataEntryList.size() != 0 )
                    {
                        Iterator lotDataEntryIter = poLineItemLotDataEntryList.iterator();
                        while( lotDataEntryIter.hasNext() )
                        {
                            PurchaseOrderLineItemLotDataEntry poLineItemDataEntry = ( PurchaseOrderLineItemLotDataEntry ) lotDataEntryIter.next();
                            if ( poLineItemDataEntry != null && ( poLineItemDataEntry.getLotNumber() == null || "".equals(poLineItemDataEntry.getLotNumber().trim())) )
                            {
                                lotDataEntryIter.remove();
                            }
                        }
                    }
                }
            }
            actionForward = "purchaseOrderLineItemLotEntryClose";
       	}
        catch ( Exception e )
        {
            Logger.error( "", e );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "", e ) );
            actionForward = "Search";
        }

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
    }

}
